//
//  UIScrollView+BLHeaderFooter.m
//  BasketBallLeagues
//
//  Created by kenshin on 4/23/17.
//  Copyright © 2017 kenshin. All rights reserved.
//

#import "UIScrollView+BLHeaderFooter.h"
#import <MJRefresh.h>

@implementation UIScrollView (BLHeaderFooter)

- (void)bl_setHeaderWithRefreshingBlock:(void (^)())refreshingBlock
{
    if (self.mj_header) {
        return;
    }
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        refreshingBlock();
    }];
    header.stateLabel.font = [UIFont systemFontOfSize:13];
    header.stateLabel.textColor = [UIColor jc_colorWithHexString:@"5b5b5b"];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
    header.lastUpdatedTimeLabel.textColor = [UIColor jc_colorWithHexString:@"a9a9a9"];
    self.mj_header = header;
}

- (void)bl_beginHeaderRefreshing
{
    if ([self bl_isHeaderRefreshing]) {
        return;
    }
    [self.mj_header beginRefreshing];
}

- (void)bl_endHeaderRefreshing
{
    if (!self.mj_header) {
        return;
    }
    [self.mj_header endRefreshing];
}

- (BOOL)bl_isHeaderRefreshing
{
    if (!self.mj_header) {
        return NO;
    }
    return self.mj_header.isRefreshing;
}

- (void)bl_setFooterWithRefreshingBlock:(void (^)())refreshingBlock
{
    [self bl_setFooterWithNoMoreTitle:@"没有更多内容了>.<"
                      refreshingBlock:refreshingBlock];
}

- (void)bl_setFooterWithNoMoreTitle:(NSString *)noMoreTitle
                    refreshingBlock:(void (^)())refreshingBlock
{
    if (self.mj_footer) {
        return;
    }
    noMoreTitle = [noMoreTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    noMoreTitle = noMoreTitle.length > 0 ? noMoreTitle : @"没有更多内容了>.<";
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        refreshingBlock();
    }];
    footer.stateLabel.font = [UIFont systemFontOfSize:13];
    footer.stateLabel.textColor = [UIColor jc_colorWithHexString:@"5b5b5b"];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:noMoreTitle forState:MJRefreshStateNoMoreData];
    self.mj_footer = footer;
}

- (void)bl_endFooterRefreshing
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer endRefreshing];
}

- (void)bl_endFooterRefreshingWithNoMoreData
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer endRefreshingWithNoMoreData];
}

- (void)bl_resetFooterToIdle
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer resetNoMoreData];
}

- (BOOL)bl_isFooterRefreshing
{
    if (!self.mj_footer) {
        return NO;
    }
    return self.mj_footer.isRefreshing;
}

- (void)bl_setFooterAutomaticallyHidden:(BOOL)automaticallyHidden
{
    if (!self.mj_footer) {
        return;
    }
    self.mj_footer.automaticallyHidden = automaticallyHidden;
}

- (void)bl_removeFooterRefreshing
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer removeFromSuperview];
    self.mj_footer = nil;
}


@end
